package com.sec.etech.employeesync.confighandler;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.sec.etech.employeesync.entity.OrgEmployeeZPrimaryLevelJob;
import com.sec.etech.employeesync.mapper.OrgEmployeeZPrimaryLevelJobMapper;
import com.sec.etech.employeesync.util.ConfigTableHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
@Slf4j
public class PrimaryLevelJobConfigHandler implements ConfigTableHandler<OrgEmployeeZPrimaryLevelJob> {

    @Resource
    private OrgEmployeeZPrimaryLevelJobMapper primaryLevelJobMapper;

    @Override
    public Long getOrCreateConfigId(String... params) {
        String code = params[0];
        String name = params[1];
        OrgEmployeeZPrimaryLevelJob primaryLevelJob = getEntityByName(name);
        if (primaryLevelJob == null) {
            primaryLevelJob = new OrgEmployeeZPrimaryLevelJob();
            primaryLevelJob.setCode(code);
            primaryLevelJob.setName(name);
            primaryLevelJob.setActive(1);
            insertEntity(primaryLevelJob);
            log.info("新增一级岗位配置:name={}", name);
        }
        return primaryLevelJob.getPrimaryLevelJobId();
    }

    @Override
    public OrgEmployeeZPrimaryLevelJob getEntityByName(String name) {
        return primaryLevelJobMapper.selectOne(Wrappers.<OrgEmployeeZPrimaryLevelJob>lambdaQuery()
                .eq(OrgEmployeeZPrimaryLevelJob::getName, name));
    }

    @Override
    public String generateNextCode() {
        return null;
    }

    @Override
    public void insertEntity(OrgEmployeeZPrimaryLevelJob entity) {
        primaryLevelJobMapper.insert(entity);
    }
}
